home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 22 / PC Actual CD 22.iso / progs / Netobj / CDK / NetObjectsFusionCDK5_97.exe / _SETUP.1 / nav_canvas.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-08  |  5.1 KB  |  177 lines

  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9. import java.awt.image.ImageObserver;
  10.  
  11. public class nav_canvas extends Canvas {
  12.    tree_entry[][] tree_grid;
  13.    int max_level;
  14.    int my_max_width;
  15.    int x_factor;
  16.    int y_factor;
  17.    Sitemapper parent_applet;
  18.    String current_selection;
  19.    boolean repeat_current;
  20.    private Image offScreenImage;
  21.    private Graphics offScreenGraphics;
  22.    private Dimension offScreenSize;
  23.  
  24.    nav_canvas(tree_entry[][] tree_grid, int max_level, int my_max_width, Sitemapper parent_applet) {
  25.       this.tree_grid = tree_grid;
  26.       this.max_level = max_level;
  27.       this.my_max_width = my_max_width;
  28.       this.parent_applet = parent_applet;
  29.    }
  30.  
  31.    tree_entry find_location(int x, int y) {
  32.       int grid_x = 0;
  33.       int grid_y = 0;
  34.       Dimension dim = ((Component)this).size();
  35.       this.x_factor = dim.width / (this.my_max_width + 1);
  36.       this.y_factor = dim.height / (this.max_level + 1);
  37.  
  38.       for(int i = 0; i < this.my_max_width + 2; ++i) {
  39.          if (x < i * this.x_factor) {
  40.             grid_x = i - 1;
  41.             break;
  42.          }
  43.       }
  44.  
  45.       for(int i = 0; i < this.max_level + 2; ++i) {
  46.          if (y < i * this.y_factor) {
  47.             grid_y = i - 1;
  48.             break;
  49.          }
  50.       }
  51.  
  52.       if (this.tree_grid[grid_x][grid_y] != null && this.tree_grid[grid_x][grid_y].publish) {
  53.          String temp_selection = this.tree_grid[grid_x][grid_y].get_url();
  54.          if (temp_selection.equals(this.current_selection)) {
  55.             this.repeat_current = true;
  56.          } else {
  57.             this.repeat_current = false;
  58.          }
  59.  
  60.          this.current_selection = temp_selection;
  61.          return this.tree_grid[grid_x][grid_y];
  62.       } else {
  63.          this.current_selection = null;
  64.          return null;
  65.       }
  66.    }
  67.  
  68.    public void paint(Graphics g) {
  69.       Dimension dim = ((Component)this).size();
  70.       g.setColor(Color.white);
  71.       g.fillRect(0, 0, dim.width, dim.height);
  72.       g.setColor(Color.black);
  73.       this.x_factor = dim.width / (this.my_max_width + 1);
  74.       this.y_factor = dim.height / (this.max_level + 1);
  75.  
  76.       for(int i = 0; i < this.my_max_width + 1; ++i) {
  77.          for(int j = 0; j < this.max_level + 1; ++j) {
  78.             if (this.tree_grid[i][j] != null && this.tree_grid[i][j].in_use) {
  79.                boolean publish = this.tree_grid[i][j].publish;
  80.                this.draw_node(g, i, j, dim, publish);
  81.                this.draw_branches(this.tree_grid[i][j], dim, g);
  82.             }
  83.          }
  84.       }
  85.  
  86.    }
  87.  
  88.    public boolean mouseUp(Event evt, int x, int y) {
  89.       if (this.current_selection != null) {
  90.          this.parent_applet.show_url(this.current_selection);
  91.       }
  92.  
  93.       return false;
  94.    }
  95.  
  96.    void draw_branches(tree_entry entry, Dimension dim, Graphics g) {
  97.       if (entry.children != null) {
  98.          tree_entry first = (tree_entry)entry.children.firstElement();
  99.          if (!first.in_use) {
  100.             return;
  101.          }
  102.  
  103.          tree_entry last = (tree_entry)entry.children.lastElement();
  104.          g.drawLine(first.grid_x * this.x_factor + this.x_factor / 4, first.grid_y * this.y_factor - this.y_factor / 2 + 5, last.grid_x * this.x_factor + this.x_factor / 4, last.grid_y * this.y_factor - this.y_factor / 2 + 5);
  105.          g.drawLine(entry.grid_x * this.x_factor + this.x_factor / 4, entry.grid_y * this.y_factor + this.y_factor / 2, entry.grid_x * this.x_factor + this.x_factor / 4, entry.grid_y * this.y_factor + this.y_factor / 2 + 5);
  106.  
  107.          for(int i = 0; i < entry.children.size(); ++i) {
  108.             tree_entry child = (tree_entry)entry.children.elementAt(i);
  109.             g.drawLine(child.grid_x * this.x_factor + this.x_factor / 4, child.grid_y * this.y_factor - this.y_factor / 2 + 5, child.grid_x * this.x_factor + this.x_factor / 4, child.grid_y * this.y_factor);
  110.          }
  111.       }
  112.  
  113.    }
  114.  
  115.    public final synchronized void update(Graphics theG) {
  116.       Dimension d = ((Component)this).size();
  117.       if (this.offScreenImage == null || d.width != this.offScreenSize.width || d.height != this.offScreenSize.height) {
  118.          this.offScreenImage = ((Component)this).createImage(d.width, d.height);
  119.          this.offScreenSize = d;
  120.          this.offScreenGraphics = this.offScreenImage.getGraphics();
  121.          this.offScreenGraphics.setFont(((Component)this).getFont());
  122.       }
  123.  
  124.       this.offScreenGraphics.fillRect(0, 0, d.width, d.height);
  125.       this.paint(this.offScreenGraphics);
  126.       theG.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
  127.    }
  128.  
  129.    void draw_node(Graphics g, int i, int j, Dimension dim, boolean publish) {
  130.       if (publish) {
  131.          g.setColor(Color.yellow);
  132.       } else {
  133.          g.setColor(Color.lightGray);
  134.       }
  135.  
  136.       g.fillRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
  137.       g.setColor(Color.black);
  138.       g.drawRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
  139.    }
  140.  
  141.    public boolean mouseMove(Event e, int x, int y) {
  142.       tree_entry entry = this.find_location(x, y);
  143.       Graphics g = ((Component)this).getGraphics();
  144.       if (entry != null && entry.publish) {
  145.          if (!this.repeat_current) {
  146.             this.update(g);
  147.             g.setColor(Color.red);
  148.             g.fillRect(entry.grid_x * this.x_factor - 1, entry.grid_y * this.y_factor - 1, this.x_factor / 2 + 2, this.y_factor / 2 + 2);
  149.             g.setColor(Color.black);
  150.             FontMetrics fm = g.getFontMetrics(g.getFont());
  151.             int str_width = fm.stringWidth(entry.get_name());
  152.             Dimension d = ((Component)this).size();
  153.             int horiz_offset = 0;
  154.             int vert_offset = 0;
  155.             if (str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 > d.width) {
  156.                horiz_offset = str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - d.width;
  157.             }
  158.  
  159.             if (entry.grid_y * this.y_factor - fm.getHeight() - 2 < 0) {
  160.                vert_offset = 0 - (entry.grid_y * this.y_factor - fm.getHeight() - 2);
  161.             }
  162.  
  163.             g.setColor(Color.white);
  164.             g.fillRect(entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - horiz_offset, entry.grid_y * this.y_factor - fm.getHeight() - 2 + vert_offset, str_width, fm.getHeight());
  165.             g.setColor(Color.black);
  166.             g.drawString(entry.get_name(), entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - horiz_offset, entry.grid_y * this.y_factor - 2 + vert_offset);
  167.             return true;
  168.          } else {
  169.             return false;
  170.          }
  171.       } else {
  172.          this.update(g);
  173.          return false;
  174.       }
  175.    }
  176. }
  177.